package com.atguigu.Sink;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.been.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

/**
 * @author wky
 * @create 2021-07-14-17:53
 */
public class Sink_Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> source = senv.socketTextStream("hadoop102", 9998);
        SingleOutputStreamOperator<String> map = source.map(new MapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                String[] split = s.split(",");
                WaterSensor waterSensor = new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));

                return JSONObject.toJSONString(waterSensor);
            }
        });
/*        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop102:9092");

        FlinkKafkaProducer flinkKafkaProducer = new FlinkKafkaProducer("sensor", new SimpleStringSchema(), properties);
        //TODO 4.kafkaSink
        jsonDStream.addSink(flinkKafkaProducer);*/
        //地址 主题 序列化器
        map.addSink(new FlinkKafkaProducer<String>("hadoop102:9092","sensor",new SimpleStringSchema()));

        senv.execute();


    }
}
